package cn.tedu.product.controller;


import cn.tedu.product.base.response.JsonResult;
import cn.tedu.product.base.response.StatusCode;
import cn.tedu.product.mapper.ProductMapper;
import cn.tedu.product.pojo.dto.ProductAddDTO;
import cn.tedu.product.pojo.entity.Product;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("v1/products/")
@Api(tags = "商品模块")
@Validated
public class ProductController {
    @Autowired(required = false)
    private ProductMapper productMapper;



    @GetMapping("getStock")
    @ApiOperation(value = "库存预警")
    public JsonResult stockwarn(@NotBlank @RequestParam Integer id){
        Integer stock=productMapper.selectStock(id);
        if (stock!=null){
            if (stock<3){
                return JsonResult.ok("库存不足，请及时补充库存");
            }else{
                return JsonResult.ok("库存充足，不必担心");
            }
        }
        return JsonResult.notok("商品ID不存在");
    }


    @GetMapping("getAllStock")
    @ApiOperation(value = "查看库存")
    public JsonResult getAllStock(){
        return JsonResult.ok(productMapper.selectAllStock());
    }


    @GetMapping("AddProduct")
    @ApiOperation(value = "添加商品")
    public JsonResult AddProduct(@RequestBody @Validated ProductAddDTO productAddDTO){
        Product product=new Product();
        BeanUtils.copyProperties(productAddDTO,product);
        int i = productMapper.AddProduct(product);
        if (i==1)
            return JsonResult.ok();
        return JsonResult.notok();
    }
}
